var Game = {
	id:0, // unique id for actors and bullets etc
	actors:{},
	bullets:{},
	particles:{},
	init:function(){
		try{
			clearInterval(Game.loopID);
		} catch(e){/*do nothing*/}
		// load collision algorithm
		Game.V = SAT.Vector;
		Game.P = SAT.Polygon;
		Game.c = document.getElementById("myCanvas").getContext("2d");
		// precalculate radian multiplier
		Game.radian = Math.PI / 180;
		Game.fps = 25;
		Game.frame = 0;
		Game.loopID = setInterval(function(){
			Game.frame++;
			Game.update();
			Game.draw();
		},1000/Game.fps);
		Game.width = $('#myCanvas').attr('width');
		Game.height = $('#myCanvas').attr('height');
		// setup players
		Game.player = Player.new();
		Game.updateObject(Game.player,{y:300,color:"#999999",a:0});
		Game.enemy = Player.new();
		Game.updateObject(Game.enemy,{y:100,color:"#666666",a:25});
	},
	update:function(){
		for(var id in Game.actors){
			Game.actors[id].collisionCheck = false;
			Game.actors[id].update();
		}
		for(var id in Game.bullets){	Game.bullets[id].update();}
		for(var id in Game.particles){	Game.particles[id].update();}
		Controls.handleInput();
	},
	draw:function(){
		Game.c.clearRect(0,0,Game.width,Game.height);
		for(var id in Game.actors){		Game.actors[id].draw();}
		for(var id in Game.bullets){	Game.bullets[id].draw();}
		for(var id in Game.particles){	Game.particles[id].draw();}
	},
	getID:function(){
		Game.id++;
		return Game.id;
	},
	applyRotate:function(obj){
		Game.c.translate(obj.x+obj.width/2,obj.y+obj.height/2);
		Game.c.rotate(obj.a * Math.PI / 180);
	},
	updateObject:function(obj,args){ // updates the variables of an obj in one line
		for(var key in args) {
			obj[key] = args[key];
		}
	},
	drawPoint:function(x,y,color,radius){
		var radius = radius || 2;
		var color = color || 'green';
		Game.c.beginPath();
		Game.c.arc(x,y,2,0,2*Math.PI,false);			
		Game.c.strokeStyle = 'green';
		Game.c.stroke();
	}
};
$(document).ready(function(){
	Game.init();
});